package com.tools.compass.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.provider.Settings;

import com.tools.compass.R;

public class CommonUtil {

	public static String getDegreeText(Context context, int degree) {
		if (degree == 0) {
			return context.getResources().getString(R.string.north);

		} else if (degree > 0 && degree < 45) {
			return context.getResources().getString(R.string.north_east_,
					degree);
		} else if (degree == 45) {
			return context.getResources().getString(R.string.east_north);

		} else if (degree > 45 && degree < 90) {
			return context.getResources().getString(R.string.east_north_,
					90 - degree);
		} else if (degree == 90) {
			return context.getResources().getString(R.string.east);

		} else if (degree > 90 && degree < 135) {
			return context.getResources().getString(R.string.east_south_,
					135 - degree);
		} else if (degree == 135) {
			return context.getResources().getString(R.string.east_south);

		} else if (degree > 135 && degree < 180) {
			return context.getResources().getString(R.string.south_east_,
					180 - degree);
		} else if (degree == 180) {
			return context.getResources().getString(R.string.south);

		} else if (degree > 180 && degree < 225) {
			return context.getResources().getString(R.string.south_west_,
					225 - degree);
		} else if (degree == 225) {
			return context.getResources().getString(R.string.west_south);

		} else if (degree > 225 && degree < 270) {
			return context.getResources().getString(R.string.west_south_,
					270 - degree);
		} else if (degree == 270) {
			return context.getResources().getString(R.string.west);

		} else if (degree > 270 && degree < 315) {
			return context.getResources().getString(R.string.west_north_,
					315 - degree);
		} else if (degree == 315) {
			return context.getResources().getString(R.string.west_north);

		} else if (degree > 315 && degree < 360) {
			return context.getResources().getString(R.string.north_west_,
					360 - degree);
		}

		return context.getResources().getString(R.string.err);
	}

	/**
	 * @param context
	 */
	public static void setSettings(Context context) {
		Intent intent = new Intent();
		intent.setAction(Settings.ACTION_SETTINGS);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			context.startActivity(intent);

		} catch (ActivityNotFoundException ex) {
		}
	}
	
	//判断网络是否存在
	public static boolean IsNetworkAvailable(Context context) {
		if (context != null) {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (cm != null) {
			    try{
			        if (cm.getActiveNetworkInfo() != null) {
	                    if (cm.getActiveNetworkInfo().isAvailable()) {
	                        return true;
	                    }
	                }    
			    }catch (Exception e) {
			        return false;
                }
				
			}
			return false;
		}
		return false ;
	}
	
    public void startSettingsActivity(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
        	context.startActivity(intent);

        } catch (ActivityNotFoundException ex) {
            intent.setAction(Settings.ACTION_SETTINGS);
            try {
            	context.startActivity(intent);
            } catch (Exception e) {
            }
        }
    }
}
